package edu.bit.water.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import edu.bit.water.entity.EntityEvaCmpWaterAnl;
import edu.bit.water.service.EntityEvaCmpWaterAnlService;



/**
 * (水平衡)企业用水分析实体表控制层
 * @author HJS
 *
 */
@Controller
@RequestMapping("/entity_eva_cmp_water_anl")
public class EntityEvaCmpWaterAnlController {
	
	@Autowired
	private EntityEvaCmpWaterAnlService entityEvaCmpWaterAnlService;
	
	/**
	 * @param entityEvaCmpWaterAnl 企业用水分析实体表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/insert")
    public Map<String, String> insert(EntityEvaCmpWaterAnl entityEvaCmpWaterAnl){
		return entityEvaCmpWaterAnlService.add(entityEvaCmpWaterAnl);
		
	}
	/**
	 * @param entityEvaCmpWaterAnl 企业用水分析实体表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
    public Map<String, String> delete(@RequestBody List<Integer> idLists){
		Map<String, String> map = new HashMap<>();
		int n = entityEvaCmpWaterAnlService.delete(idLists);
		if(n == 1)
			map.put("status", "success");
		else
			map.put("status", "failed");
		return map;
	}
	/**
	 * 
	 * @param entityEvaCmpWaterAnl 企业用水分析实体表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/update")
    public Map<String, String> update(EntityEvaCmpWaterAnl entityEvaCmpWaterAnl){
		Map<String, String> map = new HashMap<>();
		return entityEvaCmpWaterAnlService.update(entityEvaCmpWaterAnl);
		
	}
	/**
	 * 
	 * @param labelId 水分析表记录外键，也就是对应的标签表id
	 * @return 成功返回所有记录，失败或者没有记录返回null
	 */
	@ResponseBody
	@RequestMapping("/select")
    public Map<String, Object> select(int id){
		Map<String, Object> map = new HashMap<>();
		List<EntityEvaCmpWaterAnl> list = entityEvaCmpWaterAnlService.select(id);
		if(list != null) {
			map.put("status", "success");
			map.put("rows", list);
		}
		else
			map.put("status", "failed");
		return map;
	}
	/**
	 * 在计算之前先返回是否已有值
	 * @param row_id	标签表的id
	 * @param count		公式id(确定第几个公式)
	 * @return 返回json数据
	 */
	@ResponseBody
	@RequestMapping("/selectResult")
    public JSONObject selectResult(int rid,int count){
		Map<String, Object> map = new HashMap<>();
		JSONObject jsonObject = entityEvaCmpWaterAnlService.selectResult(rid, count);
		return jsonObject;
	}
	
	/**
	 * 
	 * @param jsonObject josn数据
	 * @return 返回json数据
	 */
	@ResponseBody
	@RequestMapping("/calculateResult")
    public JSONObject calculateResult(@RequestBody JSONObject jsonObject){
		Map<String, Object> map = new HashMap<>();
		int id = jsonObject.getIntValue("id");
		int count = jsonObject.getIntValue("count");
		jsonObject.remove("id");
		jsonObject.remove("count");
		JSONObject jsonObject2 = entityEvaCmpWaterAnlService.calculateResult(id, count, jsonObject);
		return jsonObject2;
	}
	
}